package com.future.mayi.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.fragment.CouponsFragment;
import com.future.mayi.widget.CouponsTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CouponsActivity extends BaseTitleActivity {
    @BindView(R.id.iv_get_coupons)
    ImageView ivGetCoupons;
    @BindView(R.id.tab_layout)
    CouponsTabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;


    private List<CouponsFragment> fragmentList;
    private List<String> listTitle;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("优惠券");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_my_coupons);
    }

    @Override
    protected void initView() {

        ViewGroup.LayoutParams layoutParams = ivGetCoupons.getLayoutParams();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = (int) (0.1867f * CommonUtils.getScreenSize(mContext)[0]);

        listTitle = new ArrayList<>(3);
        listTitle.add("未使用");
        listTitle.add("已使用");
        listTitle.add("已过期");

        fragmentList = new ArrayList<>(3);
        CouponsFragment couponsFragment = new CouponsFragment();
        couponsFragment.setType(0);
        fragmentList.add(couponsFragment);

        couponsFragment = new CouponsFragment();
        couponsFragment.setType(1);
        fragmentList.add(couponsFragment);

        couponsFragment = new CouponsFragment();
        couponsFragment.setType(10);
        fragmentList.add(couponsFragment);

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                if (listTitle != null && listTitle.size() > 0) {
                    return listTitle.get(position);
                }
                return super.getPageTitle(position);
            }
        });
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    protected void initDates() {

    }

    @Override
    protected void initListener() {

    }


    @OnClick(R.id.iv_get_coupons)
    public void onViewClicked() {
        startActivity(new Intent(this, GetCouponsActivity.class));
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (fragmentList != null && fragmentList.size() > 0)
            fragmentList.get(viewPager.getCurrentItem()).refresh();
    }
}
